var nownum = 0,
	imgArr = getElementsByClassName("pictureList")[0].getElementsByTagName("li"),
	imgLen = imgArr.length,
	numArr = getElementsByClassName("numberList")[0].getElementsByTagName("li");
//自动轮播图片
var autoplay = setInterval(function() {
	if(nownum < imgLen - 1) {
		nownum++;
	} else {
		nownum = 0;
	}
	change(nownum);//轮播图片
},10000);//10秒时延
//图片编号功能
numFunction();

function numFunction() {
	for (var i = 0; i < imgLen; i++) {
		(function(_i){
			numArr[_i].onmouseover = function() {
				clearTimeout(autoplay);//鼠标滑过清除定时
				change(_i);
				nownum = _i;
			};
			numArr[_i].onmouseout = function() {
				autoplay = setInterval(function() {
					if(nownum < imgLen - 1) {
						nownum++;
					} else {
						nownum = 0;
					}
					change(nownum);
				},6000);
			};
		})(i);
	};
}

function change(num) {
	var currentImg = getElementsByClassName("firstPic")[0];
	fadeOut(currentImg);
	removeClass(currentImg,"firstPic");
	addClass(imgArr[num],"firstPic");
	fadeIn(imgArr[num]);

	var currentNum = getElementsByClassName("firstNum")[0];
	removeClass(currentNum,"firstNum");
	addClass(numArr[num],"firstNum");
}
//透明度
function setOpacity(elem,level) { 
    if(elem.filters) { 
      	elem.style.filter = "alpha(opacity="+level+")";
    } else { 
      	elem.style.opacity = level / 100;
    }
 }
//淡入
function fadeIn(elem) {
	setOpacity(elem,0);
	for (var i = 0; i <= 20; i++) {
		(function() {
			var level = i * 5;//透明度变化值
			setTimeout(function() {
				setOpacity(elem,level);
			},i*25);//改变透明度的时间间隔
		})(i);
	};
}
//淡出
function fadeOut(elem) {
	for (var i = 0; i <= 20; i++) {
		(function() {
			var level = 100 - i * 5;
			setTimeout(function() {
				setOpacity(elem,level);
			},i*25);
		})(i);
	}
}

function getElementsByClassName(className) {
	var classArr = [];
	var tags = document.getElementsByTagName('*');
	for (var item in tags) {
		if (tags[item].nodeType == 1) {
			if (tags[item].getAttribute('class') == className) {
				classArr.push(tags[item]);
			}
		}
	}
	return classArr;
}
//是否存在class
function hasClass(obj,cls) {
    return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
//添加class
function addClass(obj,cls) {
	if(!this.hasClass(obj,cls)) {
		obj.className += cls;
	}
}
//移除class
function removeClass(obj,cls) {
	if(hasClass(obj,cls)) {
		var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
		obj.className = obj.className.replace(reg,'');
	}
}